package com.smartwebx.ha.web.admin.module.action;

import static com.smartwebx.ha.web.admin.common.HaConstant.HA_USER_SESSION_KEY;
import static com.smartwebx.ha.web.admin.common.HaConstant.LOGIN_RETURN_DEFAULT_LINK;

import java.util.Map.Entry;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.service.form.CustomErrors;
import com.alibaba.citrus.service.requestcontext.parser.ParameterParser;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.dataresolver.FormField;
import com.alibaba.citrus.turbine.dataresolver.FormGroup;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.alibaba.citrus.util.StringUtil;
import com.smartwebx.ha.biz.assign.HaAreaManager;
import com.smartwebx.ha.biz.assign.HaUserManager;
import com.smartwebx.ha.biz.rainwater.real.StationManager;
import com.smartwebx.ha.dal.assign.bean.HaArea;
import com.smartwebx.ha.dal.assign.bean.HaUser;
import com.smartwebx.ha.web.admin.common.HaUserSys;

public class LoginAction {
	@Autowired
	private HaUserManager haUserManager;
	@Autowired
	private HaAreaManager haAreaManager;
	@Autowired
	private HttpServletResponse response;
	@Autowired
	private StationManager stationManager;

	public void doOtherLogin(@Param("name") String name,
			@Param("pass") String pass, HttpSession session, Navigator nav,
			ParameterParser params) {
		Entry<Integer, HaUser> haUsre = haUserManager.loginUser(name, pass);

		if (haUsre.getKey() == 1) {
			// 在session中创建petstoreUser对象
			HaUserSys haUserSys = (HaUserSys) session
					.getAttribute(HA_USER_SESSION_KEY);
			if (haUserSys == null || haUserSys.hasLoggedIn()) {
				haUserSys = new HaUserSys();
			}
			HaUser user = haUsre.getValue();
			HaArea area = haAreaManager.findOne(user.getAreaId());

			haUserSys.upgrade(user.getName(), new String[] { "admin" },
					user.getId(), user.getAreaId(), user.getRoleId(),
					area.getCode(), area.getGroupId());

			session.setAttribute(HA_USER_SESSION_KEY, haUserSys);
			
			Cookie groupIdCookie = new Cookie("groupId", area.getGroupId()
					.toString());
			groupIdCookie.setMaxAge(3600 * 24);
			response.addCookie(groupIdCookie);

			Cookie areaIdCookie = new Cookie("areaId", area.getId().toString());
			areaIdCookie.setMaxAge(3600 * 24);
			response.addCookie(areaIdCookie);

			Cookie parentIdCookie = new Cookie("parentId", area.getParentId()
					.toString());
			parentIdCookie.setMaxAge(3600 * 24);
			response.addCookie(parentIdCookie);

			Cookie userIdCookie = new Cookie("userId", user.getId().toString());
			userIdCookie.setMaxAge(3600 * 24);
			response.addCookie(userIdCookie);

			Cookie codeCookie = new Cookie("code", user.getCode());
			codeCookie.setMaxAge(3600 * 24);
			response.addCookie(codeCookie);

			Cookie roleIdCookie = new Cookie("roleId", user.getRoleId()
					.toString());
			roleIdCookie.setMaxAge(3600 * 24);
			response.addCookie(roleIdCookie);
			
			// 跳转到return页面
			redirectToReturnPage(nav, params);
		}
	}

	public void doLogin(@FormGroup("login") HaUser userData,
			@FormField(name = "loginError", group = "login") CustomErrors err,
			Navigator nav, HttpSession session, ParameterParser params)
			throws Exception {
		Entry<Integer, HaUser> haUsre = haUserManager.loginUser(
				userData.getName(), userData.getPass());

		if (haUsre.getKey() == 1) {
			// 在session中创建petstoreUser对象
			HaUserSys haUserSys = (HaUserSys) session
					.getAttribute(HA_USER_SESSION_KEY);
			/*
			 * String station100=stationManager.getGisStations("雨量100"); String
			 * station150=stationManager.getGisStations("雨量150"); String
			 * station188=stationManager.getGisStations("雨量188"); String
			 * station280=stationManager.getGisStations("雨量280");
			 */
			if (haUserSys == null || haUserSys.hasLoggedIn()) {
				haUserSys = new HaUserSys();
			}
			HaUser user = haUsre.getValue();
			HaArea area = haAreaManager.findOne(user.getAreaId());

			haUserSys.upgrade(user.getName(), new String[] { "admin" },
					user.getId(), user.getAreaId(), user.getRoleId(),
					area.getCode(), area.getGroupId());

			session.setAttribute(HA_USER_SESSION_KEY, haUserSys);

			Cookie groupIdCookie = new Cookie("groupId", area.getGroupId()
					.toString());
			groupIdCookie.setMaxAge(3600 * 24);
			response.addCookie(groupIdCookie);

			Cookie areaIdCookie = new Cookie("areaId", area.getId().toString());
			areaIdCookie.setMaxAge(3600 * 24);
			response.addCookie(areaIdCookie);

			Cookie parentIdCookie = new Cookie("parentId", area.getParentId()
					.toString());
			parentIdCookie.setMaxAge(3600 * 24);
			response.addCookie(parentIdCookie);

			Cookie userIdCookie = new Cookie("userId", user.getId().toString());
			userIdCookie.setMaxAge(3600 * 24);
			response.addCookie(userIdCookie);

			Cookie codeCookie = new Cookie("code", user.getCode());
			codeCookie.setMaxAge(3600 * 24);
			response.addCookie(codeCookie);

			Cookie roleIdCookie = new Cookie("roleId", user.getRoleId()
					.toString());
			roleIdCookie.setMaxAge(3600 * 24);
			response.addCookie(roleIdCookie);

			/*
			 * Cookie station100Cookie = new Cookie("station100", station100);
			 * station100Cookie.setMaxAge(3600 * 24);
			 * response.addCookie(station100Cookie);
			 * 
			 * Cookie station150Cookie = new Cookie("station150", station150);
			 * station150Cookie.setMaxAge(3600 * 24);
			 * response.addCookie(station150Cookie);
			 * 
			 * Cookie station188Cookie = new Cookie("station188", station188);
			 * station188Cookie.setMaxAge(3600 * 24);
			 * response.addCookie(station188Cookie);
			 */

			/*
			 * Cookie station280Cookie = new Cookie("station280", station280);
			 * station280Cookie.setMaxAge(3600 * 24);
			 * response.addCookie(station280Cookie);
			 */

			// 跳转到return页面
			redirectToReturnPage(nav, params);
		} else {
			err.setMessage("invalidUserOrPassword");
		}
	}

	public void doLogout(HttpSession session, Navigator nav,
			ParameterParser params) throws Exception {
		// 清除session中的user
		session.removeAttribute(HA_USER_SESSION_KEY);

		// 跳转到return页面
		redirectToReturnPage(nav, params);
	}

	private void redirectToReturnPage(Navigator nav, ParameterParser params) {
		String returnURL = params.getString("return");

		if (StringUtil.isEmpty(returnURL)) {
			nav.redirectTo(LOGIN_RETURN_DEFAULT_LINK);
		} else {
			nav.redirectToLocation(returnURL);
		}
	}
}
